﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{
    class Pa : GameObject3D
    {
        VertexPositionTexture[] verts;
        VertexBuffer vertexBuffer;
        BasicEffect effect;
        Texture2D txt;
        BlocoTxt bloco;
        Vector3 position;
        float angle;
        float rotation;
        Matrix world;
        

        public Pa(Vector3 position,Vector2 size, GraphicsDevice graphics, Camera camera, Texture2D txt, float angle)
        {
            this.angle = angle;
            this.txt = txt;
            verts = new VertexPositionTexture[12];
            bloco = new BlocoTxt(0.2f, graphics, camera, txt, position);
            this.position = position;
            //pa esquerda
            verts[0] = new VertexPositionTexture(new Vector3(0, 0, -size.Y), new Vector2(1, 0));
            verts[1] = new VertexPositionTexture(new Vector3(-size.X, size.Y, 0), new Vector2(0, 1));
            verts[2] = new VertexPositionTexture(new Vector3(-size.X, -size.Y, 0), new Vector2(1, 1));

            //pa direita
            verts[3] = new VertexPositionTexture(new Vector3(0, 0, -size.Y), new Vector2(1, 0));
            verts[4] = new VertexPositionTexture(new Vector3(size.X, size.Y, 0), new Vector2(0, 1));
            verts[5] = new VertexPositionTexture(new Vector3(size.X, -size.Y, 0), new Vector2(1, 1));

            //pa de cima
            verts[6] = new VertexPositionTexture(new Vector3(0, 0, -size.Y), Vector2.Zero);
            verts[7] = new VertexPositionTexture(new Vector3(-size.Y, size.X, 0), Vector2.Zero);
            verts[8] = new VertexPositionTexture(new Vector3(size.Y, size.X, 0), Vector2.Zero);

            //pa de baixo
            verts[9] = new VertexPositionTexture(new Vector3(0, 0, -size.Y), Vector2.Zero);
            verts[10] = new VertexPositionTexture(new Vector3(size.Y, -size.X, 0), Vector2.Zero);
            verts[11] = new VertexPositionTexture(new Vector3(-size.Y, -size.X, 0), Vector2.Zero);

            vertexBuffer = new VertexBuffer(graphics, typeof(VertexPositionTexture), verts.Length, BufferUsage.None);
            vertexBuffer.SetData<VertexPositionTexture>(verts);

            this.effect = new BasicEffect(graphics);
            effect.Texture = txt;
            effect.TextureEnabled = true;
            
            
        }
        public override void Update(Camera camera)
        {
            effect.View = camera.view;
            effect.Projection = camera.projection;
            effect.World = Matrix.Identity;
            rotation += 0.02f;
            world = Matrix.Identity;
            world *= Matrix.CreateRotationZ(rotation);
            world *= Matrix.CreateRotationY(MathHelper.ToRadians(angle));
            world *= Matrix.CreateTranslation(position);

            effect.World = world;
            base.Update(camera);
        }

        public override void Draw(GraphicsDevice graphics)
        {
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphics.SetVertexBuffer(vertexBuffer);
                graphics.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, verts, 0, 4);
            }

            base.Draw(graphics);
        }
    }
}
